package com.sailfish.springbootdemo.service.db4;

import com.sailfish.springbootdemo.dao.db4.UserDao;
import com.sailfish.springbootdemo.dao.db4.OperateDao;
import com.sailfish.springbootdemo.pojo.Result;
import com.sailfish.springbootdemo.pojo.db4.Operate;
import com.sailfish.springbootdemo.pojo.db4.User;
import com.sailfish.springbootdemo.utils.CommonUtils;
import com.sailfish.springbootdemo.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OperateServiceImpl implements OperateService {

    @Autowired
    private OperateDao operateDao;

    @Autowired
    private UserDao userDao;

    @Override
    public Result getAllOperate(Integer page, Integer pageSize, String operateBigType, String nickNameCn, String operateDataType, String operateDataTypeNotes) {
        try {
            if (page == null || !CommonUtils.isNumeric(page.toString())) {
                return ResultUtil.error(500, "错误参数page", "Error Param page", null);
            }
            if (pageSize == null || !CommonUtils.isNumeric(pageSize.toString())) {
                return ResultUtil.error(500, "错误参数pageSize", "Error Param pageSize", null);
            }
            int begin = (page - 1) * pageSize;
            List<Operate> list1 = operateDao.getAllOperate(begin, pageSize, operateBigType, nickNameCn, operateDataType, operateDataTypeNotes);
            for (Operate operate : list1) {
                operate.setOperator(userDao.getUserInfoById(operate.getOperatorId()));
            }
            List<Operate> list2 = operateDao.getAllOperate(null, null, operateBigType, nickNameCn, operateDataType, operateDataTypeNotes);
            return ResultUtil.tableSuccess(page, pageSize, list2.size(), list1);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @Override
    public Boolean addOperate(Operate operate) {
        Integer operateId = operate.getOperateId();
        User userInfoById = userDao.getUserInfoById(operateId);
        if (userInfoById != null) {
            operate.setNickNameCn(userInfoById.getNickNameCn());
        }
        return operateDao.addOperate(operate) > 0;
    }
}
